<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基于Vue的路由实现跳转</title>
    <script src="../Vue.js"></script>
    <!-- 引入Vuerouter -->
    <script src="../VueRouter.js"></script>
</head>

<body>
    <div id="app">
        <router-link to="/home">首页</router-link>
        <router-link to="/news">新闻</router-link>
        <router-link to="/movie">电影</router-link>
        <!-- 路由占位符 -->
        <router-view></router-view>
    </div>

    <script>
        const home = {
            template: `<div>
                <h1>我是首页</h1>
                <button @click="goNews">跳转到新闻</button>
                </div>`,
            methods: {
                goNews() {
                    this.$router.push({ 'path': '/news', params: { id: 1 } })
                }
            }
        }
        const news = {
            template: `<h1>我是新闻</h1>`
        }
        const funny = {
            template: `<h1>我是喜剧</h1>`
        }
        const scare = {
            template: `<h1>我是惊悚</h1>`
        }
        const movie = {
            template: `<div>
                    <h1>我是电影</h1>
                    <router-link to="/movie/funny">喜剧</router-link>
                    <router-link to="/movie/scare">惊悚</router-link>
                    <!-- 路由占位符 -->
                    <router-view></router-view>
                </div>
            `
        }


        const router = new VueRouter({
            mode:'hash', // hash 或者 history
            routes: [{
                path: '/home',
                component: home
            }, {
                path: '/news',
                component: news
            }, {
                path: '/movie',
                component: movie,
                children: [
                    {
                        path: '/movie/funny',
                        component: funny
                    },
                    {
                        path: '/movie/scare',
                        component: scare
                    },
                ]
            }, {
                path: '/',
                redirect: '/home'
            }]
        })
        const vm = new Vue({
            el: '#app',
            data: {

            },
            // 并不需要这里注册组件
            // components:{
            //     home,
            //     news,
            //     movie,
            //     funny,
            //     scare
            // },
            router
        })
    </script>
</body>

</html>